package eyesInTheSkyGUI

import swing._
import scala.swing._
import event._
import java.awt.image._
import javax.swing.ImageIcon
import javax.imageio.ImageIO._
import javax.swing.table._
import java.awt.Graphics
import bj.actor.House
import bj.actor.GameServer

object HouseGUI extends SimpleSwingApplication {
  def top = new MainFrame {
    title = "House Admin Login"
      
    

    val lAdminID = new Label { text = "Adminitrator Login ID" }
    val tAdminID = new TextField { columns = 5 }
    val adminName = tAdminName
    val lAdminPW = new Label { text = "Password" }
    var tAdminPW = new PasswordField(8) 
    val bLogin = new Button { text = "Login" }
    
  //Used as padding between components
    val space0 = new Label { text = "  " }
    val space1 = new Label { text = "  " }
    val space2 = new Label { text = "  " }
    val space3 = new Label { text = "  " }
    val space4 = new Label { text = "  " }
    val label0 = new Label { text = "  " }
    val space00 = new Label { text = "  " }
    val space10 = new Label { text = "  " }
    val space20 = new Label { text = "  " }
    val space30 = new Label { text = "  " }
    val space40 = new Label { text = "  " }
    val space50 = new Label { text = "  " }
    val space60 = new Label { text = "  " }
    val label00 = new Label { text = "  " }
    
    var tableNum = 0
    var playerNum = 0
    var count = 0
    var iconH = 0
    var iconW = 0
    var prevLogoff = false

    val casImg = new Label {
      icon = new ImageIcon("Images/loginVirtualCasino.jpg")
      iconH = icon.getIconHeight()
      iconW = icon.getIconWidth()
    }

    val casImgPanel = new FlowPanel { background = java.awt.Color.GREEN }
    casImgPanel.contents += casImg

    val adminLoginPanel = new GridPanel(11, 1) {}
    adminLoginPanel.contents += space00
    adminLoginPanel.contents += space10
    adminLoginPanel.contents += lAdminID
    adminLoginPanel.contents += tAdminID
    adminLoginPanel.contents += lAdminPW
    adminLoginPanel.contents += tAdminPW
    adminLoginPanel.contents += space20
    adminLoginPanel.contents += space30
    adminLoginPanel.contents += space40
    adminLoginPanel.contents += space50
    adminLoginPanel.contents += bLogin

  //House Admin Screen
    val casImg2 = new Label {
      icon = new ImageIcon("Images/casinoLogo.jpg")
      iconH = icon.getIconHeight()
      iconW = icon.getIconWidth()
      background = java.awt.Color.GREEN
    }
    
    val buttonStartHouse: scala.swing.AbstractButton = new scala.swing.Button { text = "StartHouse " }
    val buttonStopHouse: scala.swing.AbstractButton = new scala.swing.Button { text = "StopHouse  " }
    val buttonViewTables: scala.swing.AbstractButton = new scala.swing.Button { text = "ViewTables " }
    val buttonTotal_PandL: scala.swing.AbstractButton = new scala.swing.Button { text = "Total_PandL" }
    val buttonViewPlayers: scala.swing.AbstractButton = new scala.swing.Button { text = "All Players" }
    val buttonLogoff: scala.swing.AbstractButton = new scala.swing.Button { text = "Logoff     " }

  //RollOver info
    buttonStartHouse.rolloverEnabled = true
    buttonStartHouse.tooltip = "Resets current Earnings and Bets for all Players in the House.\n " +
                               "If the StartButton is enabled - this means no Games are playing at the present!"

    buttonStopHouse.rolloverEnabled = true
    buttonStopHouse.tooltip = "End any games in progress at Tables, stops Tables, and Dealers."

    buttonViewTables.rolloverEnabled = true
    buttonViewTables.tooltip = "Displays a list of Tables with their current statuses, Earnings, and Loses. " +
      "From this list additional actions can be taken based on the status of a table."

    buttonTotal_PandL.rolloverEnabled = true
    buttonTotal_PandL.tooltip = "Displays the starting float for the House for the current day " +
      "and running totals for Earnings, Losses, and House Current Balance."

    buttonViewPlayers.rolloverEnabled = true
    buttonViewPlayers.tooltip = "Displays all players in the House. By Clicking on a Player Name, this " +
      "will display Betting history, & cureent Earnings and Losses."

    val buttonsPanel = new GridPanel(11, 1) {
      val houseButtons = new scala.swing.ButtonGroup() //not used; Couldn't figure out how to use ...
    }

    buttonsPanel.contents += buttonStartHouse
    buttonsPanel.contents += space0
    buttonsPanel.contents += buttonStopHouse
    buttonsPanel.contents += space1
    buttonsPanel.contents += buttonViewTables
    buttonsPanel.contents += space2
    buttonsPanel.contents += buttonTotal_PandL
    buttonsPanel.contents += space3
    buttonsPanel.contents += buttonViewPlayers
    buttonsPanel.contents += space4
    buttonsPanel.contents += buttonLogoff

    buttonStopHouse.enabled   = false
    buttonViewTables.enabled  = false
    buttonTotal_PandL.enabled = false
    buttonViewPlayers.enabled = false

    val lAdminName = new Label { text = "AdminName:" }
    var tAdminName = new TextField { columns = 10 }
    val lCurrHouseBal = new Label { text = "Current House Balance:" }
    var tCurrHouseBal = new TextField { columns = 10 }
    tCurrHouseBal.editable = false
    tCurrHouseBal.text = "$0"

    val houseBalStat = new FlowPanel {}
    houseBalStat.contents += lAdminName
    houseBalStat.contents += tAdminName
    houseBalStat.contents += lCurrHouseBal
    houseBalStat.contents += tCurrHouseBal
    houseBalStat.contents += space60

    runGUI

    def runGUI {
      def runLoginScreen {
        contents = new BorderPanel {
          //Login Page
          add(casImgPanel, BorderPanel.Position.West)
          val label0 = new Label { text = "  " }
          add(label0, BorderPanel.Position.Center)
          add(adminLoginPanel, BorderPanel.Position.East)
          border = Swing.EmptyBorder(30, 30, 30, 30)
        }
      }
      listenTo(bLogin) //For the Login Screen

      reactions += { // Reactions# 1
        case ButtonClicked(`bLogin`) => 
          title = "House Administration Console"
          tAdminName.text = tAdminID.text
          println(tAdminID.text)
          tAdminName.editable = false
          tCurrHouseBal.editable = false

          contents = new BorderPanel {
            add(houseBalStat, BorderPanel.Position.North)
            add(buttonsPanel, BorderPanel.Position.West)
            add(label0, BorderPanel.Position.Center)
            add(casImg2, BorderPanel.Position.East)
            border = Swing.EmptyBorder(30, 30, 30, 30)
          }

        //listenTo(button0, button1) //StartButton, StopButton, etc.
          listenTo(buttonStartHouse, buttonStopHouse, buttonViewTables,
        		   buttonTotal_PandL, buttonViewPlayers, buttonLogoff)

          reactions += { //repaint House Admin screen based on button selection to the other Screens 
            case ButtonClicked(`buttonStartHouse`)  => runStartHouse
            case ButtonClicked(`buttonStopHouse`)   => runStopHouse
            case ButtonClicked(`buttonViewTables`)  => runViewTables
            case ButtonClicked(`buttonTotal_PandL`) => runTotalPandL
            case ButtonClicked(`buttonViewPlayers`) => runViewPlayers
            case ButtonClicked(`buttonLogoff`)      => runLogoff
          } // End Reactions# 2
          
          def runStartHouse {
            println("Hello buttonStartHouse!")
            buttonStopHouse.enabled   = true
            buttonViewTables.enabled  = true
            buttonTotal_PandL.enabled = true
            buttonViewPlayers.enabled = true
            buttonStartHouse.enabled  = false
            if (count == 0) //Temp; this will be a calculated value { CurrentBal = StartingBal + (Earnings - Losses) }
              tCurrHouseBal.text = "$10,000,000.00"
              else 
                tCurrHouseBal.text = "$10,069,525.00"
            
            GameServer.main()
            //House.start
          }

          def runStopHouse {//More to come
            println("Hello buttonStopHouse!")
            buttonStartHouse.enabled = true
            buttonStopHouse.enabled = false
          }

          def runViewTables {
            title = "View All Tables"
            count = 1
            val startButton = new Button { text = "Start" }
            val stopButton = new Button { text = "Stop " }
            val rowSelected = new Button { text = "View Table Selected" }
            val tableModel = new DefaultTableModel(new Array[Array[AnyRef]](0, 5), // Data
            										   Array[AnyRef]("TableNo", "Earnings", "Losses", "Status")) // Column names
            val table = new Table(4, 5) { model = tableModel }
            
            
            tableModel.addRow(Array[AnyRef]("TableNo", "Earnings", "Losses", "Status"))
            tableModel.addRow(Array[AnyRef]("1", "$100", "$10", "Started"))
            tableModel.addRow(Array[AnyRef]("2", "$50", "$50", "Stopped"))
            tableModel.addRow(Array[AnyRef]("3", "$500", "$100", "Started"))
            tableModel.addRow(Array[AnyRef]("4", "$250", "$200", "Started"))
            tableModel.addRow(Array[AnyRef]("5", "$1050", "$500", "Stopped"))
            tableModel.addRow(Array[AnyRef]("6", "$50000", "$1000", "Started"))
            tableModel.addRow(Array[AnyRef]("7", "$100000", "$2000", "Stopped"))
            
            
            
            tAdminName.editable = false
            tCurrHouseBal.text = "$10,069,525.00"
            tCurrHouseBal.editable = false

            val startStopTblButtons = new BorderPanel {
              background = java.awt.Color.GRAY
              add(startButton, BorderPanel.Position.West)
              add(rowSelected, BorderPanel.Position.Center)
              add(stopButton, BorderPanel.Position.East)
              //contents.append(startButton, stopButton) goes with FlowPanel
              border = Swing.EmptyBorder(10, 10, 0, 10)
            }
            var tablePanel = new BoxPanel(Orientation.Vertical) {
              background = java.awt.Color.GRAY
              contents += table
              contents += startStopTblButtons
              border = Swing.EmptyBorder(70, 10, 70, 10)
            }
            println("Hello buttonViewTables!")

            def repaintViewTablesScreen {
              contents = new BorderPanel {
                add(houseBalStat, BorderPanel.Position.North)
                add(buttonsPanel, BorderPanel.Position.West)
                add(label0, BorderPanel.Position.Center)
                add(tablePanel, BorderPanel.Position.East)
                border = Swing.EmptyBorder(30, 30, 30, 30)
              }
            }

            repaintViewTablesScreen
              //Used to generate the Row number for a Table
              //Take this number & get the Table stats {Earnings, Losses, Players at table}
              val tblRow = table.selection.rows.result()
              table.listenTo(table.selection)
           /*    
              if(tableModel.getValueAt(tableNum, 3) == "Started")
              	{	stopButton.enabled  = false
              		startButton.enabled = true
              	}
              else
              	{	stopButton.enabled  = true
              		startButton.enabled = false
              	}
            */
              table.reactions += {
                case tableModel => tblRow foreach { kv => /*println("table " + kv + " was selected");*/ tableNum = kv } 
              }
              
              listenTo(startButton, stopButton, rowSelected)
              reactions += {
                case ButtonClicked(`startButton`) => println("Tbl " + tableNum + " Started"); 
                									 startTablesMsg
                case ButtonClicked(`stopButton`)  => println("Tbl " + tableNum + " Stoped")
                									 stopTablesMsg
                case ButtonClicked(`rowSelected`)  => //println("Tbl " + tableNum + " Stoped")
                									 rowSelectedMsg
              }
              
            def startTablesMsg {
            	val tblStartedMsg = new Label { text = "TABLE " + tableNum + " Started" }
            	

            	if(tableModel.getValueAt(tableNum, 3) == "Started")
            	{ 
            	  stopButton.enabled  = true
            	  startButton.enabled = false
            	}
            	table.update(tableNum, 3, "Started")
            	tablePanel = new BoxPanel(Orientation.Vertical) {
            		background = java.awt.Color.GRAY
            				contents += table
            				contents += startStopTblButtons
            				contents.append(tblStartedMsg)
            				border = Swing.EmptyBorder(70, 10, 70, 10)
            	}
            	tCurrHouseBal.text = "$10,069,525.00"
            	tCurrHouseBal.editable = false
            	repaintViewTablesScreen
            }//End startTableMsg
           
           def stopTablesMsg {
            val tblStartedMsg = new Label { text = "TABLE " + tableNum + " Stoped" }
            
            if(tableModel.getValueAt(tableNum, 3) == "Stopped")
            {	
            	stopButton.enabled  = false
                startButton.enabled = true
            }
            table.update(tableNum, 3, "Stopped")
            tablePanel = new BoxPanel(Orientation.Vertical) {
              background = java.awt.Color.GRAY
              contents += table
              contents += startStopTblButtons
              contents.append(tblStartedMsg)
              border = Swing.EmptyBorder(70, 10, 70, 10)
            }
            
            repaintViewTablesScreen
           }//End stopTableMsg
           
           def rowSelectedMsg {
                //val tblStartedMsg = new Label { text = "TABLE " + tableNum + " Stoped" }
        	val tblIconImg = new Label {
        	   icon = new ImageIcon("Images/tableInfo.jpg")
        	}
            
            //table.update(tableNum, 3, "Stopped")
            tablePanel = new BoxPanel(Orientation.Vertical) {
              background = java.awt.Color.GRAY
              contents += tblIconImg
              //contents += startStopTblButtons
              //contents.append(tblStartedMsg)
              border = Swing.EmptyBorder(70, 10, 70, 10)
            }
            repaintViewTablesScreen
           }
            
           
          }//End runViewTables
          
          

          def runTotalPandL {
            println("Hello buttonTotal_PandL!")
            count = 1
            title = "View Total Earnings & Losses for the House"
            val houseBalImg = new Label {
              icon = new ImageIcon("Images/houseBalance.jpg")
              iconH = icon.getIconHeight()
              iconW = icon.getIconWidth()
            }
            val tablePanel = new BoxPanel(Orientation.Vertical) {
              background = java.awt.Color.GRAY
              contents += houseBalImg
              border = Swing.EmptyBorder(70, 10, 70, 10)
            }
            contents = new BorderPanel {
              add(houseBalStat, BorderPanel.Position.North)
              add(buttonsPanel, BorderPanel.Position.West)
              add(label0, BorderPanel.Position.Center)
              add(tablePanel, BorderPanel.Position.East)
              border = Swing.EmptyBorder(30, 30, 30, 30)
            }
          }//End runTotalPandL

          def runViewPlayers {
            println("Hello buttonViewPlayers!")
            count = 1
            var playerRow = -1
            title = "View All Players in the House"
            val rowSelected = new Button { text = "View Player Selected" }
            
        	val playerInfoIconImg = new Label {
        	   icon = new ImageIcon("Images/playerInfo.jpg")
        	}
            
            
            val tableModel = new DefaultTableModel(new Array[Array[AnyRef]](0, 3), // Data
            										   Array[AnyRef]("TableNo", "Earnings", "Losses")) // Column names 
            val table = new Table(3, 4) { model = tableModel }
            tableModel.addRow(Array[AnyRef]("Player Name", "Earnings", "Losses"))
            tableModel.addRow(Array[AnyRef]("Don", "$100", "$10"))
            tableModel.addRow(Array[AnyRef]("Krish", "$50", "$50"))
            tableModel.addRow(Array[AnyRef]("Sean", "$500", "$100"))
            tableModel.addRow(Array[AnyRef]("Ankith", "$250", "$200"))
            tableModel.addRow(Array[AnyRef]("Venni", "$1050", "$500"))
            tableModel.addRow(Array[AnyRef]("John", "$50000", "$1000"))
            tableModel.addRow(Array[AnyRef]("Joe", "$100000", "$2000"))
            
            var tablePanel = new BoxPanel(Orientation.Vertical) {
              background = java.awt.Color.GRAY
              contents += table
              contents += rowSelected
              border = Swing.EmptyBorder(70, 10, 70, 10)
            }
            def repaint {
              contents = new BorderPanel {
                add(houseBalStat, BorderPanel.Position.North)
                add(buttonsPanel, BorderPanel.Position.West)
                add(label0, BorderPanel.Position.Center)
                add(tablePanel, BorderPanel.Position.East)
                border = Swing.EmptyBorder(30, 30, 30, 30)
              }
            }
            repaint
            //Used to generate the Row number for a Player --> Take this number & get the Player History
              val tblRow = table.selection.rows.result()
              table.listenTo(table.selection)
              table.reactions += {
                case tableModel => tblRow foreach { kv => /*println("Player at Row " + kv + " is selected");*/ playerRow=kv }
              }
            
              listenTo(rowSelected)
              reactions += {
                case ButtonClicked(`rowSelected`)  => println("Player " + playerRow + " Selected")
                									 displayPlayerInfo
              }
            
              
            def displayPlayerInfo {

            	val playerIconImg = new Label {
            		icon = new ImageIcon("Images/playerInfo.jpg")
            	}
            	tablePanel = new BoxPanel(Orientation.Vertical) {
            		background = java.awt.Color.GRAY
            				contents += playerIconImg
            				//contents += startStopTblButtons
            				//contents.append(tblStartedMsg)
            				border = Swing.EmptyBorder(70, 10, 70, 10)
            	}
            	repaint
            }
              
          }//End runViewPlayers
          

          def runLogoff { //Go back to Login Page
            title = "House Admin Login"
            println("Hello buttonLogoff!")
            
            //tAdminPW = new PasswordField
            //tAdminID.text = ""
            //for(x <- 0 to tAdminPW.password.size - 1 ) tAdminPW.password(x) = '\0'
              
            prevLogoff = true
            
          top.closeOperation()
          //runLoginScreen
          }//End runLogoff
      } // End Reactions# 1
      runLoginScreen
    } //End runGUI
  } //End top = MainFrame
} //End HouseGUI